<?php declare(strict_types=1);
// +----------------------------------------------------------------------
// | HiPHP框架[基于ThinkPHP6.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) http://www.hiphp.net
// +----------------------------------------------------------------------
// | HiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 祈陌 <3411869134@qq.com>，开发者QQ群：829699898
// +----------------------------------------------------------------------

namespace app\system\admin;
use app\common\controller\Common;
use app\system\model\SystemUser as UserModel;
use think\captcha\facade\Captcha;
/**
 * 后台入口
 * @package app\system\Entry
 */
class Entry extends Common
{
    /**
     * 登陆页面
     * @author 祈陌 <3411869134@qq.com>
     * @return mixed
     */
    public function index()
    {

        $model = new UserModel;
        $loginError = (int)session('admin_login_error');
        if ($this->request->isPost()) {
            try{
                $username = $this->request->post('username/s');
                $password = $this->request->post('password/s');
                $captcha = $this->request->post('captcha/s');
                $data = [];
                if ($loginError >= 3) {
                    if (empty($captcha)) {
                        return $this->response(0,'请输入验证码');
                    }
                    if (!captcha_check($captcha)) {
                        return $this->response(0,'验证码错误');
                    }
                }
                if (!$model->login($username, $password)) {
                    $loginError = ($loginError+1);
                    session('admin_login_error', $loginError);
                    $data['token'] = token();
                    $data['loginError'] = $loginError;

                    return $this->response(0, $model->error, (string)url('index'), $data);
                }
                session('admin_login_error', 0);

                return $this->response(1,'登陆成功，页面跳转中...', (string)url('index/index'));
            }catch (\Exception $e){
                return $this->response(0,'登陆失败');
            }

        }

        if ($model->isLogin()) {
            return redirect((string)url('system/index/index'));
        }

        $this->assign('loginError', $loginError);
        return $this->view();
    }

    public function logout(){
        $user = new UserModel();
        $user->logout();
        redirect((string)url('system/entry/index'));
    }

    public function captcha()
    {
        return Captcha::create();
    }

    public function download_lock_del(){
        if($this->request->port()){
            $group = $this->request->post('group/s');
            if(is_file($file = public_path() .'download/' . $group . '/download.lock')){
                @unlink($file);
                return $this->response(1, '已删除');
            }else{
                return $this->response(0, '删除失败，请手动执行删除[public/download/'.$group.'/download.lock]');
            }
        }
    }

}
